﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using System.Drawing.Imaging;
using System.Security.Cryptography;

namespace MapEditorMario
{
    public partial class ListScene : Form
    {
        public static String fileImage = "";
        public static String fileTxt = "";
        public static String nameFile = "";
        public static Boolean exit = false;
        ImageList imageList = new ImageList();
        ArrayList arr;
        ArrayList arrMatrix;

        public ListScene()
        {
            InitializeComponent();
            exit = false;

            //Title item
            listView_Scene.Columns.Add("Tên file Image", 100);
            listView_Scene.Columns.Add("Tên file Ma trận", 100);
            
            arr = new ArrayList();
            arrMatrix = new ArrayList();

            arr = getFileName("tiles.png");
            arrMatrix = getFileName("tiles.txt");

            //add row item to list
            for (int i = 0; i < arr.Count; i++)
            {
                String[] row = {arr[i].ToString(), arrMatrix[i].ToString()};
                ListViewItem l = new ListViewItem(row);
                listView_Scene.Items.Add(l);
            }
            
            //imageList.Images.Add(Image.FromFile("ResourceBackground/scene1.png"));
            //Image img = cutPic(new Bitmap("ImageTitle/aaaaaaaaaaa.png"), 160, 128);

            //imageList.Images.Add(Properties.Resources.bg_1);
            //listView_Scene.LargeImageList = imageList;
            //listView_Scene.LargeImageList.ImageSize = new Size(192, 128);
            //listView_Scene.Items[0].ImageIndex = 0;

        }

        private ArrayList getFileName(String extension)
        {
            DirectoryInfo dir = new DirectoryInfo(@"MapEditor/");
            ArrayList arr = new ArrayList();
            FileInfo[] file = dir.GetFiles("*" + extension);
            foreach (FileInfo fileInfo in file)
            {
                arr.Add(fileInfo);
            }
            return arr;
        }

        //private Bitmap cutPic(Bitmap bitmap, int width, int height)
        //{
        //    Rectangle cropRect = new Rectangle(0,0,width,height);
        //    Bitmap bmp = new Bitmap(bitmap);
        //    Graphics g = Graphics.FromImage(bmp);

        //    g.DrawImage(bmp, new Rectangle(0, 0, width, height),cropRect,
        //            GraphicsUnit.Pixel);

        //    return bmp;
        //}

        private void listView_Scene_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = listView_Scene.SelectedIndices[0];
            for (int i = 0; i < listView_Scene.Items.Count; i++ )
            {
                if (listView_Scene.Items[i].Selected == true)
                {
                    if (cbox_sendToBg.Checked == true)
                    {
                        fileImage = "MapEditor/" + listView_Scene.Items[i].SubItems[0].Text;
                        fileTxt = "MapEditor/" + listView_Scene.Items[i].SubItems[1].Text;
                        nameFile = listView_Scene.Items[i].SubItems[1].Text;
                    }
                    else
                    {
                        if (MessageBox.Show("Do you want to delete file?","Delete Box",MessageBoxButtons.OKCancel) == DialogResult.OK)
                        {
                            File.Delete("MapEditor/" + listView_Scene.Items[i].SubItems[0].Text);
                            File.Delete("MapEditor/" + listView_Scene.Items[i].SubItems[1].Text);
                        }
                    }
                }
            }
            
            exit = true;
            this.Dispose();
        }

        private void cbox_sendToBg_CheckedChanged(object sender, EventArgs e)
        {
            if (cbox_sendToBg.Checked)
            {
                cbox_Delete.Checked = false;
            }
            else
            {
                cbox_Delete.Checked = true;
            }
        }

        private void cbox_Delete_CheckedChanged(object sender, EventArgs e)
        {
            if (cbox_Delete.Checked)
                cbox_sendToBg.Checked = false;
            else
                cbox_sendToBg.Checked = true;
        }
    }


}
